
#include "SpecificParameters.h"
#include "Common/Strings.h"

using namespace ttg::utils;

//---------------------
//--- ParameterEnum ---
//---------------------

std::vector<std::string> ParameterEnum::parseElements(const char *elements)
{
	ttg::strings::StringSplitter sp;
	sp.setSplitter('|', false);
	sp.setString(elements);
	std::vector<std::string> res(sp.getCount());
	for (size_t i = 0; i < res.size(); i++)
		res[i] = sp[i];
	return res;
}

ParameterEnum::ParameterEnum(const char *name, const char *elements)
	:Parameter(true,	//stub (to avoid constructor collisions)
			   name,
			   ttg_internal::utils::TypeSerializer::serializeEnum(parseElements(elements)).c_str())
{
	this->elements = parseElements(elements);
	if (getCount() != 0)
		(*this) = 0;
}

ParameterEnum::ParameterEnum(const char *name, ParameterSet *owner, const char *elements)
	:Parameter(true,	//stub (to avoid constructor collisions)
			   name,
			   ttg_internal::utils::TypeSerializer::serializeEnum(parseElements(elements)).c_str(),
			   owner)
{
	this->elements = parseElements(elements);
	if (getCount() != 0)
		(*this) = 0;
}

size_t ParameterEnum::operator[] (const char *el)
{
	for (size_t i = 0; i < elements.size(); i++)
		if (elements[i] == el)
			return i;
	throw ttg::Error(ttg::Error::FATAL, "ParameterEnum", "operator[]",
					 "There aren't such elements");
}

size_t ParameterEnum::operator =(size_t index)
{
	(*(Parameter<std::string> *)this) = (*this)[index];
	return index;
}
